<?php
// +----------------------------------------------------------------------
// | Author: July
// +----------------------------------------------------------------------
// | 创建时间: 2023-12-27 20:39:42
// +----------------------------------------------------------------------
// | 文件上传驱动
// +----------------------------------------------------------------------

namespace july\julyadmin\uploaddrives;

class Drives
{
    /**
     * 获取驱动列表
     * @Author   July
     * @DateTime 2024-01-14T09:06:50+0800
     * @return   array                   驱动列表
     */
    public static function getDrives()
    {
        $files  = self::getDriveFiles("../vendor/july/julyadmin/src/uploaddrives/drives");
        $drives = [];
        foreach ($files as $fk => $fv) {
            $namespace                = "july\\julyadmin\\uploaddrives\\drives\\" . $fv;
            $drive                    = new $namespace;
            $drives[$drive->drive_id] = $drive->drive_name;
        }
        return $drives;
    }

    /**
     * 获取驱动名称
     * @Author   July
     * @DateTime 2024-01-14T09:07:20+0800
     * @param  string $drive_id 驱动id
     * @return string           驱动名称
     */
    public static function getDriveName($drive_id = '')
    {
        $files = self::getDriveFiles("../vendor/july/julyadmin/src/uploaddrives/drives");
        foreach ($files as $fk => $fv) {
            $namespace = "july\\julyadmin\\uploaddrives\\drives\\" . $fv;
            $drive     = new $namespace;
            if ($drive->drive_id == $drive_id) {
                return $drive->drive_name;
            }
        }
        return '驱动不存在';
    }

    /**
     * 获取驱动命名空间
     * @Author   July
     * @DateTime 2024-01-14T09:07:36+0800
     * @param  string $drive_id 驱动id
     * @return string           驱动名称
     */
    public static function getDriveNamesPace($drive_id = '')
    {
        $files = self::getDriveFiles("../vendor/july/julyadmin/src/uploaddrives/drives");
        foreach ($files as $fk => $fv) {
            $namespace = "july\\julyadmin\\uploaddrives\\drives\\" . $fv;
            $drive     = new $namespace;
            if ($drive->drive_id == $drive_id) {
                return ['code' => 1, 'msg' => '获取驱动驱动成功', 'data' => ['namespace' => $namespace]];
                return $drive->drive_name;
            }
        }
        return ['code' => 0, 'msg' => '驱动不存在'];
    }

    /**
     * 获取驱动文件列表
     * @Author   July
     * @DateTime 2024-01-14T09:07:49+0800
     * @param string $dir          控制器目录
     * @param array  $files        控制器文件数组
     * @param string $seach_file   要查找的文件
     * @return array $files        驱动文件列表
     */
    public static function getDriveFiles($dir = "", &$files = [], $seach_file = '')
    {
        $handle = opendir($dir);
        while ($file = readdir($handle)) {
            $type = filetype($dir . "/" . $file);
            if ($file == '.' || $file == "..") {
                continue;
            }

            if ($type == "file") {
                //如果类型为文件
                $ext = explode(".", $file);
                if (count($ext) > 1) {
                    if ($ext[count($ext) - 1] == "php") {
                        if ($seach_file) {
                            if ($file == $seach_file) {
                                $files[] = $ext[0];
                                closedir($handle);
                                return $files;
                            }
                        } else {
                            $files[] = $ext[0];
                        }
                    }
                }
            }
            if ($type == "dir") {
                //如果类型为文件夹
                self::getDriveFiles($dir . "/" . $file, $files, $seach_file);
            }
        }
        closedir($handle);
        return $files;
    }
}
